using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Logger;
using System.Text.RegularExpressions;
using FiltronASP.Helpers.Constants;

namespace FiltronASP.Models.DAO.WIX
{
    public class WixInterchangeDAO : Abstracts.AbstractGenericDAO<WixInterchange, System.Nullable<int>>,
        Interfaces.WIX.IWixInterchangeDAO
    {
        /// <summary>
        /// Metoda zwracajaca nazwe parametru bedacego kluczem glownym tabeli.
        /// </summary>
        /// <returns>nazwa klucza glownego</returns>
        protected override string GetKeyProperty()
        {
            return "interchange_id";
        }

        public IQueryable<WixInterchange> FindByForeignFilterNameEquals(string filterNumber)
        {
            filterNumber = Regex.Replace(filterNumber, @"\s", "");
            filterNumber = Regex.Replace(filterNumber, @"\.", "");
            if (filterNumber == "")
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixInterchange> filters = this._cmsEntitiesDataContext.WixInterchanges.Where(
                    inte => (inte.foreign_filter_number.ToLower().Equals(filterNumber.ToLower()) || inte.foreign_filter_trim.ToLower().Replace("-", String.Empty).Replace("/", String.Empty).Equals(filterNumber.ToLower()))
                        );
                if (!(filters.Any()))
                {
                    return null;
                }   //if
                return filters;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        public IQueryable<WixInterchange> FindByForeignFilterName(string filterNumber)
        {
            filterNumber = Regex.Replace(filterNumber, @"\s", "");
            filterNumber = Regex.Replace(filterNumber, @"\.", "");
            if (filterNumber == "")
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixInterchange> filters = this._cmsEntitiesDataContext.WixInterchanges.Where(
                    inte => (inte.foreign_filter_number.ToLower().Contains(filterNumber.ToLower()) || inte.foreign_filter_trim.ToLower().Contains(filterNumber.ToLower()))
                        ).Take(30);
                if (!(filters.Any()))
                {
                    return null;
                }   //if
                return filters;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        public IQueryable<WixInterchange> FindByForeignFilterNameLimited(string filterNumber)
        {
            filterNumber = Regex.Replace(filterNumber, @"\s", "");
            filterNumber = Regex.Replace(filterNumber, @"\.", "");
            if (filterNumber == "")
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixInterchange> filters = this._cmsEntitiesDataContext.WixInterchanges.Where(
                    inte => (inte.foreign_filter_number.ToLower().Contains(filterNumber.ToLower()) || inte.foreign_filter_trim.ToLower().Replace("-", String.Empty).Replace("/", String.Empty).Contains(filterNumber.ToLower()))
                        );
                if (!(filters.Any()))
                {
                    return null;
                }   //if
                return filters;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        public IQueryable<WixInterchange> FindByForeignFilterNameLimited(string filterNumber, Constants.TSearchInclusionMode searchMode)
        {
            filterNumber = Regex.Replace(filterNumber, @"\s", "");
            filterNumber = Regex.Replace(filterNumber, @"\.", "");
            if (filterNumber == "")
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixInterchange> filters = null;
                switch (searchMode)
                {
                    case Constants.TSearchInclusionMode.simBoth:
                        filters = this._cmsEntitiesDataContext.WixInterchanges.Where(
                            inte => (inte.foreign_filter_number.ToLower().Contains(filterNumber.ToLower()) || inte.foreign_filter_trim.ToLower().Replace("-", String.Empty).Replace("/", String.Empty).Contains(filterNumber.ToLower()))
                        ).OrderBy(inte => inte.foreign_filter_number);
                        break;
                    case Constants.TSearchInclusionMode.simExact:
                        filters = this._cmsEntitiesDataContext.WixInterchanges.Where(
                            inte => (inte.foreign_filter_number.ToLower().Equals(filterNumber.ToLower()) || inte.foreign_filter_trim.ToLower().Replace("-", String.Empty).Replace("/", String.Empty).Equals(filterNumber.ToLower()))
                        ).OrderBy(inte => inte.foreign_filter_number);
                        break;
                    case Constants.TSearchInclusionMode.simLeft:
                        filters = this._cmsEntitiesDataContext.WixInterchanges.Where(
                            inte => (inte.foreign_filter_number.ToLower().StartsWith(filterNumber.ToLower()) || inte.foreign_filter_trim.ToLower().Replace("-", String.Empty).Replace("/", String.Empty).StartsWith(filterNumber.ToLower()))
                        ).OrderBy(inte => inte.foreign_filter_number);
                        break;
                    case Constants.TSearchInclusionMode.simRight:
                        filters = this._cmsEntitiesDataContext.WixInterchanges.Where(
                            inte => (inte.foreign_filter_number.ToLower().EndsWith(filterNumber.ToLower()) || inte.foreign_filter_trim.ToLower().Replace("-", String.Empty).Replace("/", String.Empty).EndsWith(filterNumber.ToLower()))
                        ).OrderBy(inte => inte.foreign_filter_number);
                        break;
                }   //switch
                if (filters == null)
                {
                    return null;
                }   //if
                if (!(filters.Any()))
                {
                    return null;
                }   //if
                return filters;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        public IQueryable<WixInterchange> FindByForeignFilterNameLimited(string filterNumber, Constants.TSearchInclusionMode searchMode, int limit)
        {
            filterNumber = Regex.Replace(filterNumber, @"\s", "");
            filterNumber = Regex.Replace(filterNumber, @"\.", "");
            if (limit <= 0)
            {
                throw new Exception("Non-positive limit value");
            }   //if
            if (filterNumber == "")
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixInterchange> filters = null;
                switch (searchMode)
                {
                    case Constants.TSearchInclusionMode.simBoth:
                        filters = this._cmsEntitiesDataContext.WixInterchanges.Where(
                            inte => (inte.foreign_filter_number.ToLower().Contains(filterNumber.ToLower()) || inte.foreign_filter_trim.ToLower().Replace("-", String.Empty).Replace("/", String.Empty).Contains(filterNumber.ToLower()))
                        ).OrderBy(inte => inte.foreign_filter_number).Take(limit);
                        break;
                    case Constants.TSearchInclusionMode.simExact:
                        filters = this._cmsEntitiesDataContext.WixInterchanges.Where(
                            inte => (inte.foreign_filter_number.ToLower().Equals(filterNumber.ToLower()) || inte.foreign_filter_trim.ToLower().Replace("-", String.Empty).Replace("/", String.Empty).Equals(filterNumber.ToLower()))
                        ).OrderBy(inte => inte.foreign_filter_number).Take(limit);
                        break;
                    case Constants.TSearchInclusionMode.simLeft:
                        filters = this._cmsEntitiesDataContext.WixInterchanges.Where(
                            inte => (inte.foreign_filter_number.ToLower().StartsWith(filterNumber.ToLower()) || inte.foreign_filter_trim.ToLower().Replace("-", String.Empty).Replace("/", String.Empty).StartsWith(filterNumber.ToLower()))
                        ).OrderBy(inte => inte.foreign_filter_number).Take(limit);
                        break;
                    case Constants.TSearchInclusionMode.simRight:
                        filters = this._cmsEntitiesDataContext.WixInterchanges.Where(
                            inte => (inte.foreign_filter_number.ToLower().EndsWith(filterNumber.ToLower()) || inte.foreign_filter_trim.ToLower().Replace("-", String.Empty).Replace("/", String.Empty).EndsWith(filterNumber.ToLower()))
                        ).OrderBy(inte => inte.foreign_filter_number).Take(limit);
                        break;
                }   //switch
                if (filters == null)
                {
                    return null;
                }   //if
                if (!(filters.Any()))
                {
                    return null;
                }   //if
                return filters;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

    }
}